package ec.cd.common;

import java.io.Serializable;
import java.lang.Math;

public class Sensors {
    // Sensor types - closely follows Android's definitions
    public static final int ST_GYROSCOPE = 4;
    public static final int ST_ACCELEROMETER = 1;

    // Sensor's sampling rates - closely follows Android's definitions
    public static final int SSR_FASTEST = 0;
    public static final int SSR_GAME = 1;
    public static final int SSR_NORMAL = 3;
    public static final int SSR_UI = 2;

    public static enum SensorRunningMode {
        Calibration, Normal, PowerSave,
    }

    public static class SensorConfig implements Serializable {
        public int sensorType;
        public int sensorSamplingRate; // ms between each packet
        // Ranges of valid sensor values
        // In Normal mode, it contains min and max for all 3 axes
        // In PowerSave mode, it contains min1, max1, min2, max2, etc.
        public float[] ranges;
    }

    public static class SensorData implements Serializable {
        public int sensorType;
        public long timestamp;
        public float[] values; // x, y, z

        @Override
        public String toString() {
            return  values[0] + ", " + values[1] + ", " + values[2];
        }
    }

    // Compute the angles of the acceleration vector
    public static float[] calcAccTilt(float[] vals) {
        float mag = (float)Math.sqrt(
                vals[0]*vals[0] + vals[1]*vals[1] + vals[2]*vals[2]);
        float[] ret = new float[vals.length];
        for (int i = 0; i != ret.length; ++i)
            ret[i] = (float)Math.asin(vals[i]/mag);

        return ret;
    }
}
